﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Cache;
using Resto.CostControl.Core.Facade.MaterialModule.DataTransferObject;
using System.ComponentModel.DataAnnotations;

namespace Resto.CostControl.Core.Facade.SupplierModule.DataTransferObject
{
    /// <summary>
    /// 供应商合同DTO
    /// </summary>
    [Serializable]
    public class SupplierContractDTO
    {
        #region 构造函数

        public SupplierContractDTO() { }

        internal SupplierContractDTO(SupplierContract contract)
        {
            this.ID = contract.ID;
            this.Code = contract.Code;
            this.BeginDate = contract.BeginDate;
            this.EndDate = contract.EndDate;
            this.SupplierId = contract.SupplierID;
            
            List<SupplierContractItemDTO> list = new List<SupplierContractItemDTO>();
            foreach (SupplierContractItem item in contract.Items)
                list.Add(new SupplierContractItemDTO(item));

            this.Items = list.ToArray();

        }

        #endregion

        #region 基本属性

        public string ID { get; set; }

        /// <summary>
        /// 合同编号
        /// </summary>
         [Display(Name = "合同编号")]
        public string Code { get; set; }

        /// <summary>
        /// 供应商Id
        /// </summary>
        [Display(Name = "供应商")]
        public string SupplierId { get; set; }


        [Display(Name = "供应商名称")]
        public string SupplierName
        {
            get
            {
                SupplierDTO supplierDTO = SupplierDTOCache.Get(SupplierId);
                if (supplierDTO != null)
                    return supplierDTO.Name;
                else
                    return string.Empty;
            }
        }


        /// <summary>
        /// 合同开始日期
        /// </summary>
         [Display(Name = "合同开始日期")]
        public DateTime BeginDate { get; set; }

        /// <summary>
        /// 合同结束日期
        /// </summary>
         [Display(Name = "合同结束日期")]
        public DateTime EndDate { get; set; }

        public SupplierContractItemDTO[] Items { get; set; }
               
        #endregion

        #region 方法

        internal SupplierContract ToSupplierContract()
        {
            SupplierContract contract = new SupplierContract();
            CopyToSupplierContract(contract);
            return contract;
        }

        internal void CopyToSupplierContract(SupplierContract contract)
        {
            contract.Code = this.Code;
            contract.SupplierID = this.SupplierId;
            contract.BeginDate = this.BeginDate;
            contract.EndDate = this.EndDate;

            contract.ClearItems();

            foreach (SupplierContractItemDTO item in Items)            
                contract.AddItem(item.ToSupplierContractItem());
            
        }

        #endregion
    }
}
